import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @lc app=leetcode.cn id=500 lang=java
 *
 * [500] 键盘行
 */

// @lc code=start
class Solution {
    public String[] findWords(String[] words) {
        //hash
        String s1 = "qwertyuiopQWERTYUIOP";
        String s2 = "asdfghjklASDFGHJKL";
        String s3 = "zxcvbnmZXCVBNM";
        String[] s = new String[]{s1,s2,s3};
        List<String> ans = new ArrayList<>();
        Map<Character,Integer> map = new HashMap<>();
        for(String ss : s){
            for(char c : ss.toCharArray()){
                if(ss.equals(s1)){
                    map.put(c, 0);
                }
                if(ss.equals(s2)){
                    map.put(c, 1);
                }
                if(ss.equals(s3)){
                    map.put(c, 2);
                }
            }
        }
        for(String ss : words){
            char[] c = ss.toCharArray();
            int a = map.get(c[0]);
            int i = 1;
            for(; i < c.length; i++){
                if(map.get(c[i]) != a){
                    break;
                }
            }
            if(i == c.length){
                ans.add(ss);
            }
        }
        //将ArrayList类转换成String[]类,转换成数组, 数组内类型为String,数组大小为ans.size()
        return ans.toArray(new String[ans.size()]);
    }
}
// @lc code=end

